<?php include("functions.php"); checkLogin(); verifUserIsAdmin();
include_once('module/notes/modele.php');


if(isset($_GET['admin']) AND $_GET['admin'] == 'delete')
{
    goDeleteNotes($_GET['id']); $envoi = 0;
}
else {
    $send[] = verificationFormulaire($_POST['titre'], $rgxTitre, 'Erreur dans le nom', false);
    $_POST['titre'] = utf8_decode($_POST['titre']);
    if(isset($_POST['public']) AND ($_POST['public'] == 'on')){ $_POST["public"] = 1;}
    else{ $_POST["public"] = 0; }
    
    while(preg_match("#<p>#", $_POST["texte"]))
    {
        $_POST["texte"] = preg_replace("#<p>#", "", $_POST["texte"]);
    }
    while(preg_match("#<\/p>#", $_POST["texte"]))
    {
        $_POST["texte"] = preg_replace("#<\/p>#", "", $_POST["texte"]);
    }
    
    /*ENVOI FLOOD BDD*/
    $time = time();
    $previousTime = selectFlood($_SERVER['REMOTE_ADDR']) + 10;
    if($time <= $previousTime){
        $send[]= "Veuillez patientez avant l'envoi d'un nouveau message.";
    }
    /*fin ENVOI FLOOD BDD*/

    $envoi = verifSend($send);
}
if ($envoi AND isset($_GET['go']) AND $_GET['go'] == 'edit')
{
    goEditNotes($_POST['id'], $_POST['titre'], $_POST["lien"], $_POST['texte'], $_POST["public"]);
    insertFlood($_SERVER['REMOTE_ADDR']);
}
elseif ($envoi AND isset($_GET['go']) AND $_GET['go'] == 'add')
{
    goAjoutNotes($_POST['titre'], $_POST["lien"], $_POST['texte'], $_SESSION["ID"], $_POST["public"]);
    insertFlood($_SERVER['REMOTE_ADDR']);
}

include_once('module/notes/vue/envoi.php');